package com.example.riseofthedefender;

import javax.microedition.khronos.opengles.GL10;

public class Player extends Square {

	private int lastSwipe = 0;

	public static final int maxYV = -20;
	
	public static final int Player_wHlv = 32;
	public static final int Player_hHlv = 53;

	private static final int STATE_STAND = 0;
	private static final int STATE_SLIDE = 1;
	private static final int STATE_FLYING = 10;
	private static final int STATE_ATTACK_AIR_1 = 20;
	private static final int STATE_SHOOT_AIR_1 = 30;
	private static final int STATE_WALL_STICK = 40;
	private int STATE = STATE_STAND;

	private boolean facingRight = true;

	public Player() {
		wHlv = Player_wHlv;
		hHlv = Player_hHlv;
	}

	public void move() {
		yV -= 0.3f;
		if(yV < maxYV)
			yV = maxYV;
		pos.y += yV;
		if (Ground.collideWith(pos.x, pos.y, wHlv * 2, hHlv * 2) != -1) {
			pos.y -= yV;
			xV *= 0.95;
			if (Math.abs(xV) < 0.005) {
				xV = 0;
				STATE = STATE_STAND;
			} else {
				STATE = STATE_SLIDE;
			}
			yV = 0;
		} else {
			STATE = STATE_FLYING;
		}
		pos.x += xV;
		int collideIndex = Ground.collideWith(pos.x, pos.y, wHlv * 2, hHlv * 2);
		if (collideIndex != -1) {
			if(xV > 0)
			{
				pos.x = Ground.arrayList.get(collideIndex).left - (wHlv * 2) - 0.1f;
				xV = 1;
			}
			else if(xV < 0)
			{
				pos.x = Ground.arrayList.get(collideIndex).left + (Ground.arrayList.get(collideIndex).wHlv * 2) + 0.1f;
				xV = -1;
			}
			if(STATE == STATE_FLYING && yV <=0)
			{
				STATE = STATE_WALL_STICK;
				
			}	
		}
	}

	public void update() {

		updateInput();

		switch (STATE) {
		case STATE_SLIDE:
		case STATE_STAND:
			move_apply_touch_force();
			move();
			jump_1();
			air_attack_1();
			// air_shoot_1();
			break;
		case STATE_ATTACK_AIR_1:
			move();
			stopAttack();
			break;
		case STATE_SHOOT_AIR_1:
			move();
			stopShooting();
			break;
		case STATE_FLYING:
			move_apply_touch_force();
			move();
			// air_attack_1();
			air_shoot_1();
			break;
		case STATE_WALL_STICK:
			move();
			jump_wall_1();
			break;
		default:
			break;
		}
		if (xV > 0)
			facingRight = true;
		else if (xV < 0)
			facingRight = false;
		
		Camera.setTarget(pos.x+wHlv-Camera.cWidthH,pos.y+hHlv-Camera.cHeightH);
		Soul.collideWith(pos.x, pos.y, wHlv * 2, hHlv * 2);
	}

	private void updateInput() {
		if (lastSwipe == GameVar.swipeCount)
			return;

		switch (STATE) {
		case STATE_STAND:
		case STATE_SLIDE:
			GameVar.t_jump = true;
			GameVar.t_melee = true;
			break;
		case STATE_ATTACK_AIR_1:

			break;
		case STATE_SHOOT_AIR_1:

			break;
		case STATE_WALL_STICK:
			GameVar.t_wall_jump = true;
			
		case STATE_FLYING:
			GameVar.t_shoot = true;
			break;
		default:
			break;
		}

		lastSwipe = GameVar.swipeCount;
	}

	private void stopAttack() {
		if (SwordThing.duration <= 0) {
			STATE = STATE_FLYING;
		}
	}

	private void stopShooting() {
		if (GunThing.duration <= 0) {
			STATE = STATE_FLYING;
		}
	}

	private void air_shoot_1() {
		if (GameVar.t_shoot) {
			GunThing.shoot(pos);
			STATE = STATE_SHOOT_AIR_1;
			GameVar.t_shoot = false;
		}
	}

	private void air_attack_1() {
		if (GameVar.t_melee) {
			SwordThing.swing();
			SwordThing.pos.x = pos.x + wHlv;
			SwordThing.pos.y = pos.y + hHlv;
			STATE = STATE_ATTACK_AIR_1;
			GameVar.t_melee = false;
		}
	}

	private void move_drop_down() {
		if (GameVar.touching) {
			float goodAngle = -45;
			if (GameVar.touchAngleD < goodAngle
					&& GameVar.touchAngleD > -180 - goodAngle) {
				if (GameVar.touchDistance > 3) {
					yV = Math.min(
							yV,
							Math.max(GameVar.touchVector.x
									* GameVar.touchDistance / GameVar.cHeight
									* 20, -10));
				}
			}
		}
	}

	private void move_apply_touch_force() {
		if (GameVar.touching) {
			if (GameVar.touchDistance > 3) {
				xV = GameVar.touchVector.x * GameVar.touchDistance / GameVar.cHeight * 45;
				xV = Math.min(Math.abs(xV), 15) * (xV > 0? 1 : -1);
			}
		}
	}

	private void jump_1() {
		if (GameVar.t_jump) {
			float goodAngle = 20;
			if (GameVar.touchAngleD > goodAngle
					&& GameVar.touchAngleD < 180 - goodAngle) {
				if (GameVar.touchDistance > 3) {
					xV = GameVar.touchVector.x * GameVar.touchDistance
							/ GameVar.cHeight * 45;
					yV = GameVar.touchVector.y * GameVar.touchDistance
							/ GameVar.cHeight * 45;
					xV = (Math.min(Math.abs(xV), 15) * (xV < 0 ? -1 : 1));// clamp
																			// value
					yV = (Math.min(Math.abs(yV), 15) * (yV < 0 ? -1 : 1));// clamp
																			// value
				}
			}
			GameVar.t_jump = false;
		}
	}

	private void jump_wall_1() {
		if (GameVar.t_wall_jump) {
			float goodAngle = 20;
//			if (GameVar.touchAngleD > goodAngle
//					&& GameVar.touchAngleD < 180 - goodAngle) {
				if (GameVar.touchDistance > 3) {
					xV = GameVar.touchVector.x * GameVar.touchDistance
							/ GameVar.cHeight * 45;
					yV = GameVar.touchVector.y * GameVar.touchDistance
							/ GameVar.cHeight * 45;
					xV = (Math.min(Math.abs(xV), 15) * (xV < 0 ? -1 : 1));// clamp
																			// value
					yV = (Math.min(Math.abs(yV), 15) * (yV < 0 ? -1 : 1));// clamp
																			// value
				}
//			}
			GameVar.t_wall_jump = false;
		}
	}

	
	private void keepInBound() {
		if (pos.y + hHlv * 2 > GameVar.cHeight) {
			pos.y = GameVar.cHeight - hHlv * 2;
			yV = 0;
		}
		if (pos.x + wHlv * 2 > GameVar.cWidth)
			pos.x = GameVar.cWidth - wHlv * 2;
		else if (pos.x < 0)
			pos.x = 0;
	}

	public void draw(GL10 gl) {
		gl.glLoadIdentity();
		if (facingRight) {
			gl.glTranslatef(-Camera.pos.x,-Camera.pos.y,0.0f);
			gl.glTranslatef(pos.x + wHlv, pos.y + hHlv, 0.0f);
			// rotate, then
			gl.glScalef(70, 70, 1.0f);
		} else {
			gl.glTranslatef(-Camera.pos.x,-Camera.pos.y,0.0f);
			gl.glTranslatef(pos.x + wHlv, pos.y + hHlv, 0.0f);
			// rotate, then
			gl.glScalef(-70, 70, 1.0f);
		}

		switch (STATE) {
		case STATE_STAND:
			gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			break;
		case STATE_SLIDE:
			gl.glColor4f(1.0f, 1.0f, 0, 1.0f);
			break;
		case STATE_WALL_STICK:
			gl.glColor4f(1.0f, 0, 1.0f, 1.0f);
			break;
		case STATE_FLYING:
			gl.glColor4f(0, 1.0f, 1.0f, 1.0f);
			break;
		default:
			break;
		}
		gl.glBindTexture(GL10.GL_TEXTURE_2D, TextureManager.GIRL_1.textures[0]);

		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
		
		gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	}
}
